function [dat]=r_amt(parameter,cruise)
% ---- Read AMT parameter

dirAMT='/media/disk/OBSERVATIONS/CRUISES/AMT/';

% ---- Create the Structure ...        
dat = struct('dummy',1);
 

switch cruise
    case 'amt4' % April/May 1997        
        BODC_ref=[502181:502215,...
            502180,...
            502217:502224,...
            502216,...
            502225:502245];
    case 'amt5' % Sept/Oct 1997       
        BODC_ref=[502261:502291];        
    case 'amt6' % May/Jun 1998
        BODC_ref=[502010:502038,...
            502006,...
            502061,...
            502039:502042,...
            502007,...
            502043:502046,...
            502008,...
            502047:502050,...
            502009,...
            502001:502005];
    case 'amt6b' % Apr 1998
        BODC_ref=[504483,...
            504481:504482,...
            504484:504505];
    case 'amt7' % Sept/Oct 1998
        BODC_ref=502301:502333;
    case 'amt8' % Apr/Jun 1999
        BODC_ref=502381:502418;
    case 'amt9' % Sept/Oct 1999
        BODC_ref=[502427:502456,...
            502421:502426,...
            502419:502420];
    case 'amt10' % Apr/May 2000
        BODC_ref=504126:504165;
    case 'amt11' % Sept/Oct 2000
        BODC_ref=[504081:504088,...
            504241,...
            504089:504125];        
    otherwise
        disp('Unknown cruise ...')
        return
end
% -- For the deepest CTD ... 
maxlength=10;
for ilen=1:length(BODC_ref)
    if ~exist([dirAMT,num2str(BODC_ref(ilen)),'.csv'],'file')
        error('The data file does not exist. Please double check the path in r_amt.m')
    end
    fid=fopen([dirAMT,num2str(BODC_ref(ilen)),'.csv']);    
    C=textscan(fid,'%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n',...
        'delimiter',',',...
        'treatAsEmpty',{'N','T'},...
        'HeaderLines',4);
    fclose(fid);       
    if (length(C{1})>maxlength)
        maxlength=length(C{1});
    end
end
% -- Intialization
pressure(1:length(BODC_ref),1:maxlength)=NaN;
sal(1:length(BODC_ref),1:maxlength)=NaN;
temp(1:length(BODC_ref),1:maxlength)=NaN;
chl(1:length(BODC_ref),1:maxlength)=NaN;
lon(1:length(BODC_ref),1:maxlength)=NaN;
lat(1:length(BODC_ref),1:maxlength)=NaN;
time(1:length(BODC_ref),1:maxlength)=NaN;
BODC_Id(1:length(BODC_ref),1:maxlength)=NaN;


for ilen=1:length(BODC_ref)
    % -- Read the file
    fid=fopen([dirAMT,num2str(BODC_ref(ilen)),'.csv']);
    
    A=textscan(fid,'%s%s%s%n%s%s%s%n%s',...
        'delimiter',',',...
        'HeaderLines',1);
    % BODC_Id
    interm=A{4};
    BODC_Id(ilen,:)=interm(1);
    clear interm
    
    % Latitude
    interm=A{6};
    la=char(interm(1));
    if (isempty(findstr(char(la),'S'))==1) % North
       lat(ilen,:)=str2double(la(1:2))+ str2double(la(4:5))/60 + str2double(la(7:8))/3600;
    else % South
       lat(ilen,:)=-(str2double(la(1:2))+ str2double(la(4:5))/60 + str2double(la(7:8))/3600);
    end
    clear interm
    
    % Longitude
    interm=A{7};
    lo=char(interm(1));
    if (isempty(findstr(char(lo),'W'))==1) % East
       lon(ilen,:)=str2double(lo(1:3))+ str2double(lo(5:6))/60 + str2double(lo(8:9))/3600;
    else % West
       lon(ilen,:)=-(str2double(lo(1:3))+ str2double(lo(5:6))/60 + str2double(lo(8:9))/3600);
    end
    clear interm
    
    % Date (In MATLAB Serial date number - datenum)
    interm=A{9};
    tps=char(interm(1));
    time(ilen,:)=datenum(str2double(tps(7:10)),str2double(tps(4:5)),str2double(tps(1:2)),str2double(tps(12:13)),str2double(tps(15:16)), str2double(tps(18:19)));
    clear interm
    
    % ---- to detect the index (column) of variables following the 3rd
    % header line with name of variables.
    fseek(fid, 0, 'bof');
    B=textscan(fid,'%s\n',...
        'HeaderLines',2);    
    a=char(B{:});
    
    ip=((strfind(a(1,:),'PRESPR01')-1)/10*2)+1;
    
    is=((strfind(a(1,:),'PSALST01')-1)/10*2)+1;
    
    it=((strfind(a(1,:),'TEMPST01')-1)/10*2)+1;
    
    if (~isempty(strfind(a(1,:),'CPHLPR01')))
        ic=((strfind(a(1,:),'CPHLPR01')-1)/10*2)+1;
    end
    
    imax=(size(a,2)+2)/10*2;
    
    %whos
    
    fclose(fid);
    
    fid=fopen([dirAMT,num2str(BODC_ref(ilen)),'.csv']);
    
    formatt='';
    for i=1:imax
        formatt=[formatt,'%n'];
    end
    
    C=textscan(fid,formatt,...
        'delimiter',',',...
        'treatAsEmpty',{'N','T'},...
        'HeaderLines',4);
    fclose(fid);
    
    % -- Column for depth (dB)
    pressure(ilen,1:length(C{1}))=C{ip};
    
    % -- Column for salinity (psu)
    sal(ilen,1:length(C{1}))=C{is}';
    
    % -- Column for temperature (deg C)
    temp(ilen,1:length(C{1}))=C{it}';
    
    if (~isempty(strfind(a(1,:),'CPHLPR01')))
        % -- Column for chlorophyll (mg.m-3)
        chl(ilen,1:length(C{1}))=C{ic}';
    end
    
 
end

% -- Remove flagged values ...
sal(sal <= 0)=NaN;
temp(temp <= 0)=NaN;
chl(chl <= 0)=NaN;

if (strcmp(parameter,'T'))
    dat.data=temp;
elseif (strcmp(parameter,'S'))
    dat.data=sal;
elseif (strcmp(parameter,'CHL'))
    dat.data=chl;
end
dat.londata=lon;
dat.latdata=lat;
dat.depthdata=pressure;
dat.timedata=time;
    
    
    
% Conversion from dBars to meters.
dat.depthdata = sw_dpth(dat.depthdata,dat.latdata);

% ---- Clean the Structure ...
dat=rmfield(dat,'dummy');    